<?php
/**
 * 所属项目 OnePlus.
 * 开发者: 想天
 * 创建日期: 3/12/14
 * 创建时间: 12:49 PM
 * 版权所有 想天工作室(www.ourstu.com)
 */

namespace Ucenter\Controller;

use Think\Controller;

class ReportController extends BaseController
{
    protected $mTalkModel;

    public function _initialize()
    {
        parent::_initialize();
//        $this->mTalkModel = D('Talk');
        $this->setTitle('医务中心');
    }

    public function index()
    {
        if (IS_POST) {
//            var_dump($_POST);
            $uids = I('post.categories');
            if (!is_array($uids)) {
                $this->error('操作出错,用户数据格式不正确');
            }
            $data['user_id'] = $uids[0];
            $data['pictures'] = implode(',', I('post.pictures'));
            $data['pdfs'] = implode(',', I('post.pdfs'));
            $data['doctor_id'] = is_login();
            $data['content']=I('post.content','','op_t');
            $data['update_time'] = date('Y-m-d H:i:s');
//            $data['add_time']=date('Y-m-d H:i:s');
            $id = M('UserReport')->add($data);
            if ($id)
                $this->success('添加成功!', U('rlist'));
            else
                $this->error(M('UserReport')->getError());
        } else {
            //获取病人列表
            $data = M('DoctorUser')->alias('du')
                ->field('um.id,m.nickname,um.username,du.id as did')
                ->join('__MEMBER__ m on m.uid=du.user_id', 'left')
                ->join('__UCENTER_MEMBER__ um on um.id=du.user_id', 'left')
                ->where(array('du.doctor_id' => is_login()))->select();
//            var_dump($data);
            foreach ($data as $v) {
                M('DoctorUser')->save(array('id'=>$v['did'],'user_id' => $v['id'], 'username' => $v['username'], 'nickname' => $v['nickname']));
//                var_dump(M('DoctorUser')->getlastSql());
            }
            $this->assign('users', $data);
//            var_dump($data,M('DoctorUser')->getLastSql());
            $this->assign('tabHash', 'report');
            $this->display();
        }
    }
    public function rlist(){
        $userId=I('get.userId');
        $doctorId=is_login();
        $model=M('UserReport');
        $page=I('get.page',1,'intval');
        $perPage=C('LIST_ROWS')<20?20:C('LIST_ROWS');
        $users=M('DoctorUser')->getFieldByDoctorId($doctorId,'id as did,doctor_id,user_id as id,username,nickname');
        if($userId){
            $this->assign('userId',$userId);
            $where['user_id']=$userId;
        }
        $where['doctor_id']=$doctorId;
        $count=$model->where($where)->count();
        $totalPage=$count/$perPage;
        if($page!=1 && $page>$totalPage)
            $this->redirect();
        $Page=new \Think\Page($count,$perPage);
        $show=$Page->show();

        $list=$model->where($where)->page($page,$perPage)->select();

        $this->assign('page',$show);
        $this->assign('users',$users);
        $this->assign('list',$list);
        $this->assign('tabHash', 'reportlist');

        $this->display();
    }
}